#ifndef XLCOMM_NET_HTTPSERVER_H_
#define XLCOMM_NET_HTTPSERVER_H_

#include <functional>

#include "xlcomm/define.h"
#include "xlcomm/net/event_loop.h"
#include "xlcomm/net/http/http_request.h"
#include "xlcomm/net/http/http_response.h"
#include "xlcomm/net/tcp_server.h"
#include "xlcomm/noncopyable.h"

namespace xlcomm {
namespace net {

class XLCOMM_API HttpServer : noncopyable {
 public:
  using HttpRequestCallback =
      std::function<void(const HttpRequest&, HttpResponse*)>;

  HttpServer(EventLoop* loop, const InetAddress& listen_add);

  void Start() { server_.Start(); }

  void set_http_request_cb(const HttpRequestCallback& cb) {
    http_request_cb_ = cb;
  }

 private:
  void OnConnect(const TcpConnectionPtr& conn);
  void OnMessage(const TcpConnectionPtr& conn, Buffer* buff,
                 Timestamp recv_time);
  void OnRequest(const TcpConnectionPtr& conn, const HttpRequest& req);

  EventLoop* loop_;
  TcpServer server_;
  HttpRequestCallback http_request_cb_;
};

}  // namespace net
}  // namespace xlcomm

#endif  // XLCOMM_NET_HTTPSERVER_H_